package com.itheima.goods.service.impl;

import com.changgou.goods.pojo.Brand;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.changgou_common.Util.PinYinUtils;
import com.itheima.goods.dao.BrandMapper;
import com.itheima.goods.service.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 品牌列表查询
     *
     * @return
     */
    @Override
    public List<Brand> findList() {

        List<Brand> brandList = brandMapper.selectAll();
        return brandList;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public Brand findById(Integer id) {

        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 添加品牌
     *
     * @param brand
     */
    @Transactional
    @Override
    public void insertBrand(Brand brand) {

        String name = brand.getName();
        if (StringUtils.isBlank(name)) {
            throw new RuntimeException("参数非法");
        }

        // 1 判断 填写 品牌首字母
        String letter = brand.getLetter();
        if (StringUtils.isBlank(letter)) {  // 没传参数
            letter = PinYinUtils.getFirstLetter(name);  //  name ? null
        } else {
            // 转成大小存储到数据库
            letter = letter.toUpperCase();
        }
        brand.setLetter(letter);

        brandMapper.insertSelective(brand);
    }

    /**
     * 修改品牌
     *
     * @param brand
     */
    @Transactional
    @Override
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * 删除品牌
     *
     * @param id
     */
    @Transactional
    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 品牌条件查询
     *
     * @param serachMap
     * @return
     */
    @Override
    public List<Brand> list(Map<String, Object> serachMap) {

        Example example = new Example(Brand.class);
        brandMapper.selectByExample(example);
        //内部类
        Example.Criteria criteria = example.createCriteria();
        //封装当前查询条件
        if (serachMap != null) {
            //模糊查询
            if (serachMap.get("name") != null && !"".equals(serachMap.get("name"))) {

                criteria.andLike("name", "%" + serachMap.get("name") + "%");
            }
            //按照品牌首字母查询精确查询
            if (serachMap.get("letter") != null && !"".equals(serachMap.get("letter"))) {


                criteria.andEqualTo("letter", serachMap.get("letter"));
            }

        }

        return brandMapper.selectByExample(example);
    }


    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Brand> findPage(int page, int size) {

        PageHelper.startPage(page, size);

        return (Page<Brand>) brandMapper.selectAll();
    }

    /**
     * 分页条件查询
     *
     * @param stringObjectMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Brand> findPages(Map stringObjectMap, Integer page, Integer size) {

        Example example = new Example(Brand.class);
        brandMapper.selectByExample(example);
        //内部类
        Example.Criteria criteria = example.createCriteria();
        //封装当前查询条件
        if (stringObjectMap != null) {
            //模糊查询
            if (stringObjectMap.get("name") != null && !"".equals(stringObjectMap.get("name"))) {

                criteria.andLike("name", "%" + stringObjectMap.get("name") + "%");
            }
            //按照品牌首字母查询精确查询
            if (stringObjectMap.get("letter") != null && !"".equals(stringObjectMap.get("letter"))) {


                criteria.andEqualTo("letter", stringObjectMap.get("letter"));
            }

        }

       return (Page<Brand>) brandMapper.selectByExample(example);

    }

  /*  @Override
    public List<Brand> hightpageList(Map serachMap) {

        Example example = new Example(Brand.class);
        brandMapper.selectByExample(example);
        //内部类
        Example.Criteria criteria = example.createCriteria();
        //封装当前查询条件
        if (serachMap != null) {
            //模糊查询
            if (serachMap.get("name") != null && !"".equals(serachMap.get("name"))) {

                criteria.andLike("name", "%" + serachMap.get("name") + "%");
            }
            //按照品牌首字母查询精确查询
            if (serachMap.get("letter") != null && !"".equals(serachMap.get("letter"))) {


                criteria.andEqualTo("letter", serachMap.get("letter"));
            }
            //设置高亮
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.field("title");
            highlightBuilder.preTags("<font color='red'>");
            highlightBuilder.postTags("</font>");



        }


        return brandMapper.selectByExample(example);


    }*/
    /*
        //获取搜索请求
        SearchRequest searchRequest = new SearchRequest("goods");
        //生成搜索器
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        //查询品牌为手机的数据
        MatchQueryBuilder query = QueryBuilders.matchQuery("title", "手机");

        searchSourceBuilder.query(query);
        //设置高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();

        *//**
     * 1.高亮三要素
     *//*
        highlightBuilder.field("title");
        highlightBuilder.preTags("<font color='red'>");
        highlightBuilder.postTags("</font>");
      *//*  TermsAggregationBuilder agg = AggregationBuilders.terms("goods_brand").field("brandName").size(50);
        searchSourceBuilder.aggregation(agg);*//*
        searchSourceBuilder.highlighter(highlightBuilder);
        //根据搜索器搜索获取的请求资源
        searchRequest.source(searchSourceBuilder);
        //在根据设置的条件筛选资源
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        //记录资源数并遍历词条获取的资源
        SearchHits hits = search.getHits();
        long value = hits.getTotalHits().value;
        System.out.println("总数" + value);

        SearchHit[] hits1 = hits.getHits();
        List<Goods> goodsList = new ArrayList<>();
        for (SearchHit documentFields : hits1) {
            //将字符串转json
            String sourceAsString = documentFields.getSourceAsString();
            Goods goods = JSON.parseObject(sourceAsString, Goods.class);
            //获取高亮结果
            Map<String, HighlightField> highlightFields = documentFields.getHighlightFields();
            HighlightField title = highlightFields.get("title");
            Text[] fragments = title.fragments();
            //替换
            goods.setTitle(fragments[0].toString());
            goodsList.add(goods);
        }

        for (Goods object : goodsList) {
            System.out.println(object);
        }
}*/


}